/**
 * 项目名称（软件名称）：忠旺机械物资管理系统(mms)
 * 研发设计单位：忠旺机械设备制造有限公司机械设计院智能控制设计科设计与研发。
 * 版权所有：忠旺机械设备制造有限公司
 * 注：本项目的一切技术专利，使用规范归解释权由忠旺机械设备制造有限公司所有。
 * 软件版本：Ver 1.0.0
 * 文件路径：com.zhongwang.mms.module.purchase.controller
 * 创建时间：2020年03月31日 11时28分
 **/
package com.zhongwang.mms.module.purchase.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.PAllocationRecord;
import com.zhongwang.mms.module.purchase.model.*;
import com.zhongwang.mms.module.purchase.service.PurAllocationManageService;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName： AllocationManageController
 * @Description： 调拨控制层
 * @Author： 徐一贺
 * @Date： 2020/3/31 11:28
 * @Version： V1.0
 **/
@Controller
@RequestMapping("/purchase/allocationManage")
public class PurAllocationManageController extends BaseController {

    @Autowired
    private PurAllocationManageService purAllocationManageService;
    @Autowired
    private LogService logService;

    @GetMapping("index")
    /**
     *@FunctionName: index
     *@Description: 跳转到调拨出库页面
     *@Author: 徐一贺
     *@CreateDate: 2020/3/31 11:30
     *@Phone: 18241927380
     *@Param: [model] 页面渲染模型
     *@Return: java.lang.String
     *@Version: v1.2.0
     */
    public String index(Model model) {
        //获取出库日期
        model.addAttribute("currentDate", purAllocationManageService.getCurrentDate());
        //制单人
        model.addAttribute("makePerson", ShiroUtils.getUser().getName());
        return "/purchase/purWarManage/AllocationManage/out/page";
    }


    @PostMapping("/save")
    @ResponseBody
    @RequiresPermissions({"purwar:puralloutwar:edit"})
    /**
     *@FunctionName: save
     *@Description: 创建未执行调拨单，并返回新的调拨单
     *@Author: 徐一贺
     *@CreateDate: 2020/4/6 9:29
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult save(PInventoryQueryModel param) {
       return purAllocationManageService.save(param);
    }


    @PostMapping("/del")
    @ResponseBody
    @RequiresPermissions({"purwar:puralloutwar:edit"})
    /**
     *@FunctionName: del
     *@Description: 删除调拨单，并返回新的调拨单列表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/6 9:29
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version:
     */
    public JsonResult del(PInventoryQueryModel param) {
        PAllocationRecord pAllocationRecord = purAllocationManageService.findPAllocationRecordByPrimaryKey(param.getArdId());
        String odrCode = pAllocationRecord.getArdFormCode();
        purAllocationManageService.delPAllocationRecordByPrimaryKey(pAllocationRecord);
        List<PAllocationRecordModel> pInventoryModelList = purAllocationManageService.findAllocationOrderListByPDBCode(odrCode);
        logService.saveLog("删除调拨单", "调拨单号：" + odrCode);
        return jsonResult(pInventoryModelList);
    }

    @PostMapping("/delChild")
    @ResponseBody
    @RequiresPermissions({"purwar:puralloutwar:edit"})
    /**
     *@FunctionName: delChild
     *@Description: 删除领料单
     *@Author: 徐一贺
     *@CreateDate: 2020/4/6 18:49
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version:
     */
    public JsonResult delChild(PInventoryQueryModel param) {
        PAllocationRecord pAllocationRecord = purAllocationManageService.findPAllocationRecordByPrimaryKey(param.getArdId());
        String odrCode = pAllocationRecord.getArdFormCode();
        boolean result = purAllocationManageService.delPAllocationRecord(pAllocationRecord);
        List<PAllocationRecordModel> pInventoryModelList = purAllocationManageService.findAllocationOrderListByPDBCode(odrCode);
        logService.saveLog("删除调拨单", "调拨单号：" + odrCode);
        return jsonResult(pInventoryModelList);
    }


    @PostMapping("/delNoCreateData")
    @ResponseBody
    @RequiresPermissions({"purwar:puralloutwar:edit"})
    /**
     *@FunctionName: delNoCreateData
     *@Description: 删除未创建的数据
     *@Author: 徐一贺
     *@CreateDate: 2020/4/7 11:14
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version:
     */
    public JsonResult delNoCreateData(PInventoryQueryModel param) {
        PAllocationRecord pAllocationRecord = purAllocationManageService.findPAllocationRecordByPrimaryKey(param.getArdId());
        String odrCode = pAllocationRecord.getArdFormCode();
        boolean result = purAllocationManageService.delPAllocationRecord(odrCode);
        logService.saveLog("删除未创建调拨单", "调拨单号：" + odrCode);
        return JsonResult.success();
    }


    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions({"purwar:puralloutwar:edit"})
    /**
     *@FunctionName: add
     *@Description: 添加一条调拨记录数据
     *@Author: 徐一贺
     *@CreateDate: 2020/4/6 12:57
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult add(PAllocationRecordModel param) {
        //1.验证数量
        boolean result = purAllocationManageService.checkNum(param);
        if (result) {
            //2.验证通过返回添加成功
            PAllocationRecordModel pAllocationRecordModel = purAllocationManageService.createPAllocationRecord(param);
            logService.saveLog("创建调拨单", "调拨单号：" + param.getArdFormCode());
            return JsonResult.success("添加成功！", pAllocationRecordModel);
        }
        return JsonResult.fail("添加失败，所添数量超过最小剩余数量，被拆项目的物料数量最少保留0.01！");
    }


    @PostMapping("/findOrderTableData")
    @ResponseBody
    /**
     *@FunctionName: findOrderTableData
     *@Description: 获取获取调拨成品库入库记录表数据
     *@Author: 徐一贺
     *@CreateDate: 2020/4/6 14:03
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version:1.0.0
     */
    public JsonResult findOrderTableData(PAllocationRecordModel param) {
        String odrCode = param.getArdFormCode();
        List<PAllocationRecordModel> pAllocationRecordModelList = purAllocationManageService.findAllocationOrderListByPDBCodeAndArdType(odrCode);
        return jsonResult(pAllocationRecordModelList);
    }

    @PostMapping("/update")
    @ResponseBody
    @RequiresPermissions({"purwar:puralloutwar:edit"})
    /**
     *@FunctionName: update
     *@Description: 更新修改
     *@Author: 徐一贺
     *@CreateDate: 2020/4/6 17:43
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult update(PAllocationRecordModel param) {
        PAllocationRecordModel pAllocationRecordModel = purAllocationManageService.update(param);
        if (pAllocationRecordModel != null) {
            return JsonResult.success("修改成功！", pAllocationRecordModel);
        }
        return JsonResult.fail("修改失败，所添数量超过最小剩余数量，被拆项目的物料数量最少保留0.01！");
    }

    @PostMapping("/create")
    @ResponseBody
    @RequiresPermissions({"purwar:puralloutwar:edit"})
    /**
     *@FunctionName: create
     *@Description: 保存调拨单
     *@Author: 徐一贺
     *@CreateDate: 2020/4/6 19:20
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version:
     */
    public JsonResult create(PAllocationRecordCreateModel param) {
        String msg = purAllocationManageService.checkListNum(param);
        if (msg != null) {
            return JsonResult.fail("保存失败," + msg + "！");
        }
        POrderPrintModel pOrderPrintModel = purAllocationManageService.create(param);
        return JsonResult.success("保存成功！", pOrderPrintModel);
    }

    @PostMapping("/updatePAllocationRecord")
    @ResponseBody
    @RequiresPermissions({"purwar:puralloutwar:edit"})
    /**
     *@FunctionName: updatePAllocationRecord
     *@Description: 更新领料人，领料车间
     *@Author: 徐一贺
     *@CreateDate: 2020/4/7 7:47
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult updatePAllocationRecord(PAllocationRecordModel param) {
        if (!param.getArdFormCode().equals("")) {
            purAllocationManageService.updatePAllocationRecord(param);
        }
        return JsonResult.success("修改成功！");
    }

    @PostMapping("findNoCreateOrder")
    @ResponseBody
    /**
     *@FunctionName: findNoCreateOrder
     *@Description: 获取为创建完成的列表数据
     *@Author: 徐一贺
     *@CreateDate: 2020/4/7 11:12
     *@Phone: 18241927380
     *@Param: []
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult findNoCreateOrder() {
        List<PAllocationRecordModel> noCreateOrderList = purAllocationManageService.findNoCreateOrderList(ShiroUtils.getUser().getAccount());
        return jsonResult(noCreateOrderList);
    }

    @PostMapping("/addAllocationOutWarehouseRecordData")
    @ResponseBody
    @RequiresPermissions({"purwar:puralloutwar:edit"})
    /**
     *@FunctionName: addAllocationOutWarehouseRecordData
     *@Description: 添加领料出库数据
     *@Author: 徐一贺
     *@CreateDate: 2020/4/7 12:56
     *@Phone: 18241927380
     *@Param: []
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version:
     *
     */
    public JsonResult addAllocationOutWarehouseRecordData(PAllocationRecordModel param) {
        return purAllocationManageService.addAllocationOutWarehouseRecordData(param);
    }


    @PostMapping("/delAllocationOutWarehouseRecordData")
    @ResponseBody
    @RequiresPermissions({"purwar:puralloutwar:edit"})
    /**
     *@FunctionName: delAllocationOutWarehouseRecordData
     *@Description: 删除领料单
     *@Author: 徐一贺
     *@CreateDate: 2020/4/7 13:23
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult delAllocationOutWarehouseRecordData(PAllocationRecordModel param) {
        return JsonResult.success("",purAllocationManageService.delAllocationOutWarehouseRecordData(param));
    }

    @PostMapping("loadAllocationRecord")
    @ResponseBody
    /**
     *@FunctionName: loadAllocationRecord
     *@Description: 根据主键加载调拨入库 调拨出库 领料单数据
     *@Author: 徐一贺
     *@CreateDate: 2020/4/7 13:36
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version:
     */
    public JsonResult loadAllocationRecord(PAllocationRecordModel param) {
        List<PAllocationRecordModel> allocationOutWarehouseRecordData = purAllocationManageService.loadAllocationRecord(param);
        return jsonResult(allocationOutWarehouseRecordData);
    }

    @PostMapping("/projectCodeISExist")
    @ResponseBody
    /**
     *@FunctionName: projectCodeISExist
     *@Description: 验证项目号是否存在
     *@DesignIdea: 验证项目号是否存在
     *@Author: 徐一贺
     *@CreateDate: 2020/5/25 8:08
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Throw:
     *@Version: v 1.0.0
     */
    public JsonResult projectCodeISExist(PAllocationRecordModel param) {
        if(purAllocationManageService.projectCodeISExist(param)){
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @PostMapping("/findAllocationList")
    @ResponseBody
    /**
     *@FunctionName: findAllocationList
     *@Description: 获取调拨数据
     *@DesignIdea: 根据调拨单号和调拨类型获取调拨数据
     *@Author: 徐一贺
     *@CreateDate: 2020/5/25 14:58
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Throw: 无异常抛出
     *@Version: v 1.0.0
     */
    public JsonResult findAllocationList(PAllocationRecordModel param) {
        startPage();
        return jsonResult(purAllocationManageService.findAllocationList(param));
    }

    @PostMapping("/findAllocationSelfContainAndChildrenList")
    @ResponseBody
    /**
     *@FunctionName: findAllocationSelfContainAndChildrenList
     *@Description: 根据ardPid获取子级以及自己的数据列表
     *@Author: 徐一贺
     *@CreateDate: 2020/5/25 16:41
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: v 1.0.0
     */
    public JsonResult findAllocationSelfContainAndChildrenList(PAllocationRecordModel param) {
        startPage();
        return jsonResult(purAllocationManageService.findAllocationSelfContainAndChildrenList(param));
    }


    @PostMapping("/codeExist")
    @ResponseBody
   /**
    *@FunctionName: codeExist
    *@Description: 验证项目号是否存在
    *@DesignIdea: 验证项目号是否存在
    *@Author: 徐一贺
    *@CreateDate: 2020/5/26 10:02
    *@Phone: 18241927380
    *@Param: [param]
    *@Return: java.lang.String
    *@Version:  v 2.0.0
    */
    public String codeExist(VWProject param) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", purAllocationManageService.codeExist(param));
        String resultString = "";
        ObjectMapper mapper = new ObjectMapper();
        try {
            resultString = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resultString;
    }
}